import { reactive } from 'vue'
import { get, post } from '@/http'
import { transfromForBackend, filterOutType } from '@/utils/searchTransform'
import { cloneDeep, omit, assign } from 'lodash-es'
import type { Autocomplete } from './types'
import { Page } from '@/types/storeModule'

// 菜单
// export function getMenus() {
//   return get('/menu')
// }

// 权限
export function getPrivilegesApi(params: any) {
    return post('/research/api-backMenu/privilege-list', params)
}

// 搜索项
export function getSearchDataApi(type: string, params: any) {
    return post(`/research/api-${type}/list`, params)
}

// 获取指定搜索项的下拉选项列表
export function getSearchItemOptionApi(type: string) {
    return get(`/search_item/${type}/option`)
}

// 获取指定搜索项的autocomplete选项列表
export function getSearchItemAutocompleteApi(type: string, params: Autocomplete) {
    return get(`/search_item/${type}/autocomplete`, params)
}

// 获取标准数据autocomplete选项列表
export function getItemAutocompleteApi(uri: string, params: any) {
    return post(`/research/${uri}`, params)
}

// 列表
export function getTableDataApi(url: string, params: any) {
    let paramsData = {}
    if (params && params.page && params.page.currentPage && params.page.pageSize) {
        const data = cloneDeep(params)
        // console.log('data===', data);
        const page = reactive<Page>({
            currentPage: data.currentPage,
            pageSize: data.pageSize,
            totalCount: data.totalCount,
            sortField: data.sortField,
            order: data.order
        })
        const pageParams = { page: page }
        const condition = transfromForBackend(data.condition) // 把搜索组件绑定的值转换为后台需要的格式
        // console.log('condtion===', condition);
        const conditionParams = filterOutType(condition) // 传给后台时过滤掉type属性
        // console.log('conditionParams===', conditionParams);
        const remainderParams = omit(data, ['condition'])
        // console.log('remainderParams===', remainderParams);
        paramsData = assign(pageParams, conditionParams, remainderParams)
    } else {
        paramsData = params
    }
    // console.log('paramsData===', paramsData);
    return post(url, paramsData)
}
